/*
 * @Author: 一路向阳 tt_sunzhenfeng@163.com
 * @Date: 2024-07-15 15:20:06
 * @LastEditors: 一路向阳 tt_sunzhenfeng@163.com
 * @LastEditTime: 2024-07-15 16:16:12
 * @FilePath: \encode-hooks-react\gulpfile.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */

const gulp = require('gulp');
const ts = require('gulp-typescript');
const babel = require('gulp-babel');
const del = require('del');

gulp.task('clean', async function () {
  await del('lib/**');
  await del('es/**');
  await del('dist/**');
});

//TODO 生成ESM
gulp.task(`es`, function () {
  const tsProject = ts.createProject(`tsconfig.hooks.json`, {
    module: 'ESNext',
  });

  return tsProject.src().pipe(tsProject()).pipe(babel()).pipe(gulp.dest(`es/`));
});

gulp.task('cjs', function () {
  return gulp
    .src(['./es/**/*.js'])
    .pipe(
      babel({
        configFile: '../../.babelrc',
      }),
    )
    .pipe(gulp.dest('lib/'));
});

// 生成声明文件
gulp.task('declaration', function () {
  const tsProject = ts.createProject(`tsconfig.hooks.json`, {
    // 生成声明文件
    declaration: true,
    // 只生成DTS文件
    emitDeclarationOnly: true,
  });

  return tsProject.src().pipe(tsProject()).pipe(gulp.dest(`es/`)).pipe(gulp.dest(`lib/`));
});

gulp.task('copyReadme', async function () {
  await gulp.src('../../README.md').pipe(gulp.dest('../../packages/hooks'));
});

exports.default = gulp.series(`clean`, `es`, `cjs`, `declaration`, `copyReadme`);
